<!--
 * @Author: Allen
 * @Date: 2021-01-13 15:47:12
 * @Description: 抱怨身处黑暗，不如提灯前行。愿你在自己存在的地方，成为一束光，照亮世界的一角
 * @LastEditTime: 2021-03-03 14:38:58
 * @LastEditors: Allen
 * @FilePath: \WMS\src\views\WMS\claimDemageManage\claimDemageLaunch\index.vue
 * @Additional: 附加类容
-->

<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="true"
      :dynamicIsColumnDrop="true"
      :callbackObj="callbackObj"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-button type="primary" size="mini" class="el-icon-edit" @click="edit(scope.row.index)">编辑</el-button>
      </template>
    </one-table-template>
    <edit
      :dynamicEditRowData="editRowData"
      :popupsVisible="editPopupsVisible"
      :key="editPopupsKey"
      :popupsState="editPopupsState"
      @close="close"
      :editData="editData"
    ></edit>
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { claimDemageAPI } from "@/api/wms/claimDemage";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
export default {
  name: "stockQuery",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    Edit: () => import("./edit")
  },
  mounted() {
    this.querySelect();
  },
  watch: {
    "formField.WAREHOUSE_IDS": {
      handler: function() {
        var wArr = [];
        if (this.formField.WAREHOUSE_IDS == "") {
          this.tableComponents[0].options.map(item => {
            wArr.push(item.value);
          });
        }
        var WAREHOUSE_IDS_NEW = "";
        WAREHOUSE_IDS_NEW = wArr.join(",");
        if (WAREHOUSE_IDS_NEW != "") {
          this.formField.WAREHOUSE_IDS_NEW = WAREHOUSE_IDS_NEW;
        } else {
          this.formField.WAREHOUSE_IDS_NEW = "";
        }
      }
    }
  },
  data() {
    return {
      // 网格查询API配置对象
      apiConfig: claimDemageAPI.queryClaim,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          clickEvent: () => this.add(),
          name: "add",
          position: "left",
          text: "新增"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.launchs(),
          name: "add",
          position: "left",
          text: "发起"
        },
        {
          compKey: "btnKey5",
          type: "",
          size: "small",
          clickEvent: () => this.delete(),
          name: "deleteSheet",
          position: "left",
          text: "结案"
        },
        {
          compKey: "btnKey6",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "导出"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: true,
          options: [],
          span: 6
        },
        {
          compKey: "compKey2",
          labelName: "索赔单号",
          codeField: "CLAIM_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          span: 6
        },
        {
          compKey: "compKey3",
          labelName: "备件编号",
          textField: "PART_NO",
          codeField: "PART_NO",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsPartInfoTwo"),
          type: "propuss",
          span: 6,
          isMust: true,
          returnCodeField: "PART_NO",
          returnTextField: "PART_NO",
          isMul: false
        },
        {
          compKey: "compKey4",
          labelName: "索赔类型", //仓库名称
          codeField: "CLAIM_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [],
          span: 6
        },
        {
          compKey: "compKey5",
          labelName: "异常类型", //仓库名称
          codeField: "ERROR_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [],
          span: 6
        },
        {
          compKey: "compKey6",
          labelName: "责任方", //仓库名称
          codeField: "RESPONSIBLE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [],
          span: 6
        },
        {
          compKey: "compKey7",
          labelName: "索赔状态", //仓库名称
          codeField: "STATUS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [],
          span: 6
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作" /*操作*/,
          codeField: "controlBtn",
          width: 100,
          align: "center",
          fixed: true,
          isComponent: true,
          comps: [
            {
              compKey: "propKey2",
              align: "center",
              labelName: "编辑" /*编辑*/,
              codeField: "editControlBtn",
              clickEvent: this.edit,
              component: () => import("@/components/org/linkButton")
            }
          ]
        },
        {
          prop: "CLAIM_CODE",
          label: "索赔单号",
          align: "center"
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          align: "center"
        },
        {
          prop: "PART_NO",
          label: "备件编号",
          align: "center"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          align: "center"
        },
        {
          prop: "CLAIM_NAME",
          label: "索赔类型",
          align: "center"
        },
        {
          prop: "ERROR_NAME",
          label: "异常类型",
          align: "center"
        },
        {
          prop: "CLAIM_QTY",
          label: "索赔数量",
          align: "center"
        },
        {
          prop: "RESPONSIBLE_NAME",
          label: "责任方",
          align: "center"
        },
        {
          prop: "STATUS_NAME",
          label: "索赔状态",
          align: "center"
        }
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        CLAIM_CODE: "",
        PART_NO: "",
        CLAIM_TYPE: "",
        ERROR_TYPE: "",
        RESPONSIBLE: "",
        STATUS: "",
        STATUS_CODE: "1"
      },
      unFormField: {},
      editData: {},
      callbackObj: {
        //  当选择项发生变化时会触发该事件
        onCheckSelectChange: selection => {
          this.checkTableList = selection;
        }
      },
      // 选中的表格数据
      checkTableList: [],

      // 控制初始化页面
      initTemplate: false
    };
  },
  methods: {
    delete() {
      if (this.checkTableList.length > 0) {
        this.$confirm("确定要结案吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(() => {
            request({
              url: "/mp/wm/vl/warehouse/claim/CloseCaseClaim.do",
              method: "post",
              data: {
                INITIATECLAIM: this.checkTableList
              }
            }).then(res => {
              if (res.result === "1") {
                this.queryTable();
                this.$message({
                  message: "结案成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.debugMsg ? res.debugMsg : res.msg,
                  type: "warn"
                });
              }
            });
          })
          .catch(err => {
            console.info(err);
          });
      } else {
        this.$message({
          message: "请选择一条数据"
        });
      }
    },
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {}
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.editData.UserByWHBox = res.listRow;
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_IDS = res.listRow[0].WAREHOUSE_ID;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });
      request({
        url: "/mp/wm/vl/warehouse/claim/queryStatus.do",
        method: "post",
        data: {
          STATUS: "1"
        }
      }).then(res => {
        res.listRow.map(item => {
          this.tableComponents[6].options.push({
            label: item.STATUS_NAME,
            value: item.STATUS
          });
        });
      });
      // 异常类型
      request({
        url: "/mp/wm/vl/warehouse/claim/queryErrorType.do",
        method: "post",
        data: {}
      }).then(res => {
        this.editData.ErrorType = res.listRow;
        res.listRow.map(item => {
          this.tableComponents[4].options.push({
            label: item.ERROR_NAME,
            value: item.ERROR_TYPE
          });
        });
      });
      // 索赔类型
      request({
        url: "/mp/wm/vl/warehouse/claim/queryClaimType.do",
        method: "post",
        data: {}
      }).then(res => {
        this.editData.ClaimType = res.listRow;
        res.listRow.map(item => {
          this.tableComponents[3].options.push({
            label: item.CLAIM_NAME,
            value: item.CLAIM_TYPE
          });
        });
      });
      // 责任方
      request({
        url: "/mp/wm/vl/warehouse/claim/queryResponsible.do",
        method: "post",
        data: {}
      }).then(res => {
        this.editData.Responsible = res.listRow;
        res.listRow.map(item => {
          this.tableComponents[5].options.push({
            label: item.RESPONSIBLE_NAME,
            value: item.RESPONSIBLE
          });
        });
      });
      // 索赔状态
    },
    launchs() {
      if (this.checkTableList.length <= 0) {
        this.$message({
          message: "请勾选一条数据",
          type: "wran",
          uration: 2000
        });
      } else {
        var INITIATECLAIM = [];
        this.checkTableList.map(item => {
          INITIATECLAIM.push({ CLAIM_CODE: item.CLAIM_CODE });
        });
        request({
          url: "/mp/wm/vl/warehouse/claim/initiateClaim.do",
          method: "post",
          data: {
            INITIATECLAIM: INITIATECLAIM
          }
        }).then(res => {
          if (res.result == 1) {
            this.$message({
              message: "发起成功",
              type: "success",
              uration: 2000
            });
            this.queryTable();
            this.checkTableList = [];
          } else {
            this.$message({
              message: res.debugMsg ? res.debugMsg : res.msg,
              type: "warn",
              uration: 2000
            });
            this.checkTableList = [];
          }
        });
      }
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/warehouse/claim/exportClaim.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "索赔发起导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    }
  }
};
</script>
